﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaseUnits
{
    public class Grid : IPowerGrid
    {
        HashSet<IPowerPlant> powerPlantHash;
        int powerGridId;
        string description;
        string country;

        public Grid(int powerGridId, string description, string country)
        {
            this.powerGridId = powerGridId;
            this.powerPlantHash = new HashSet<IPowerPlant>();
            this.description = description;
            this.country = country;
        }
        public HashSet<IPowerPlant> PowerPlants
        {
            get { return this.powerPlantHash; }
        }
        public double GetOutgoing(DateTime date)
        {
            double total = 0;
            foreach (IPowerPlant p in this.powerPlantHash)
            {
                total += p.GetOutgoing(date);
            }
            return total;
        }
        public double TotalOutgoing(DateTime dateFrom, DateTime dateTo)
        {
            double total = 0;
            foreach (IPowerPlant p in this.powerPlantHash)
            {
                total += p.TotalOutgoing(dateFrom, dateTo);
            }
            return total;
        }
        public SortedList<DateTime, IPowerUnit> DateSortedPowerUnits()
        {
            //List<IPowerUnit> sortedList = new List<IPowerUnit>();
            SortedList<DateTime, IPowerUnit> sortedList = new SortedList<DateTime, IPowerUnit>();
            Random r = new Random();
            foreach (IPowerPlant p in this.powerPlantHash)
            {
                foreach (IPowerUnit pu in p.PowerUnits)
                {
                    if (!sortedList.ContainsKey(pu.BuildDate))
                    {
                        sortedList.Add(pu.BuildDate, pu);
                    }
                    else
                    {
                        sortedList.Add(pu.BuildDate.AddDays(r.Next(1, 360)), pu);
                    }
                }
            }
            return sortedList;
        }

        public List<IPowerUnit> SortedPowerUnits(DateTime date)
        {
            List<IPowerUnit> sortedList = new List<IPowerUnit>();
            SortedList<int, IPowerUnit> sortedList2 = new SortedList<int, IPowerUnit>();
            foreach (IPowerPlant p in this.powerPlantHash)
            {
                foreach (IPowerUnit pu in p.PowerUnits)
                {
                    sortedList.Add(pu);
                    //sortedList2.Add(pu.Priority(date), pu);
                }
            }
            CompareByPriority<IPowerUnit> comparer = new CompareByPriority<IPowerUnit>(date);
            sortedList.Sort(comparer);
            return sortedList;
        }

        public void AddPowerPlant(IPowerPlant powerPlant)
        {
            this.powerPlantHash.Add(powerPlant);
        }

        public int GridId
        {
            get { return this.powerGridId; }
        }

        public string Description
        {
            get { return this.description; }
        }
        public override string ToString()
        {
            return this.Description;
        }
    }
}
